package com.ytjj.qmyx.admin.controller;

import com.alibaba.druid.util.StringUtils;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.ytjj.common.api.CommonResult;
import com.ytjj.qmyx.admin.annotation.OperLog;
import com.ytjj.qmyx.admin.annotation.oplog.OperationProductLogger;
import com.ytjj.qmyx.admin.annotation.oplog.annotation.OperationLog;
import com.ytjj.qmyx.admin.constants.SysLogConstants;
import com.ytjj.qmyx.admin.model.request.goods.GetEvaluationFeedbackRequest;
import com.ytjj.qmyx.admin.model.request.goods.GetEvaluationInfoRequest;
import com.ytjj.qmyx.admin.model.request.goods.GoodsRequest;
import com.ytjj.qmyx.admin.model.response.goods.GoodsResponse;
import com.ytjj.qmyx.admin.model.response.goods.ProductSpecResponse;
import com.ytjj.qmyx.admin.service.GoodsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 新商品控制器
 *
 * 
 *  *
 **/
@RestController
@RequestMapping("/goodsTwo")
@Slf4j
public class GoodsTwoController {

    @Resource
    private GoodsService goodsService;


    @GetMapping("/getGoodsInfo/{goodsId}")
    @OperLog(operModule = "商品管理模块", operDesc = "查看商品详情信息", operType = SysLogConstants.OPERATION_SELECT)
    public CommonResult getGoodsInfo(@PathVariable Integer goodsId) {
        GoodsResponse goodsInfo = goodsService.getGoodsInfo(goodsId);
        goodsInfo.setCostPrice(new BigDecimal(0));
        goodsInfo.setPlatformData(null);
        goodsInfo.setBankerColumnId(null);
//        goodsInfo.setWarehouseName(null);
        goodsInfo.getSpec().stream().forEach(item -> {
            item.setCostPrice(new BigDecimal(0));
        });
        return CommonResult.success(goodsInfo);
    }

    @PostMapping("/updateGoods")
//    @OperLog(operModule = "商品管理模块", operDesc = "编辑商品详情信息", operType = SysLogConstants.OPERATION_UPDATE)
    @OperationLog(optModule = SysLogConstants.PRODUCT, optType = SysLogConstants.OPERATION_UPDATE, desc = "编辑商品详情信息")
    public CommonResult updateGoods(@Validated @RequestBody GoodsRequest goodsRequest) {
        try {
            Map<String, Object> map = getLog(goodsRequest);
            OperationProductLogger.log(map);
        } catch (Exception e) {
            log.error("拼装日志信息异常{}", e);
        }
        goodsService.updateGoodsTwo(goodsRequest);

        return CommonResult.success();
    }



    private Map<String, Object> getLog(GoodsRequest goodsRequest) {
        //获取修改前的数据
        GoodsResponse goodsInfo = goodsService.getGoodsInfo(goodsRequest.getId());
        Map<String, Object> map = Maps.newHashMap();
        map.put("msg", "编辑商品详情信息");
        //判断以下内容修改前后对比
        if ((null == goodsInfo.getPrice() ? BigDecimal.ZERO : goodsInfo.getPrice()).compareTo(null == goodsRequest.getPrice() ? BigDecimal.ZERO : goodsRequest.getPrice()) != 0) {
            map.put("商品售价", "修改前后：" + goodsInfo.getPrice() + "-->" + goodsRequest.getPrice());
        }

        if ((null == goodsInfo.getCostPrice() ? BigDecimal.ZERO : goodsInfo.getCostPrice()).compareTo(null == goodsRequest.getCostPrice() ? BigDecimal.ZERO : goodsRequest.getCostPrice()) != 0) {
            map.put("成本价", "修改前后：" + goodsInfo.getCostPrice() + "-->" + goodsRequest.getCostPrice());
        }

        if ((null == goodsInfo.getCurPrice() ? BigDecimal.ZERO : goodsInfo.getCurPrice()).compareTo(null == goodsRequest.getCurPrice() ? BigDecimal.ZERO : goodsRequest.getCurPrice()) != 0) {
            map.put("直接购买价", "修改前后：" + goodsInfo.getCurPrice() + "-->" + goodsRequest.getCurPrice());
        }

        if ((null == goodsInfo.getOriginalPrice() ? BigDecimal.ZERO : goodsInfo.getOriginalPrice()).compareTo(null == goodsRequest.getOriginalPrice() ? BigDecimal.ZERO : goodsRequest.getOriginalPrice()) != 0) {
            map.put("市场价(划线价)", "修改前后：" + goodsInfo.getOriginalPrice() + "-->" + goodsRequest.getOriginalPrice());
        }
        if (!StringUtils.isEmpty(goodsInfo.getProductName()) && !goodsInfo.getProductName().equals(goodsRequest.getProductName())) {
            map.put("商品名称", "修改前后：" + goodsInfo.getProductName() + "-->" + goodsRequest.getProductName());
        }
        if (null != goodsInfo.getSpecialId() && !goodsInfo.getSpecialId().equals(goodsRequest.getSpecialId())) {
            map.put("专区选择", "修改前后：" + goodsInfo.getSpecialId() + "-->" + goodsRequest.getSpecialId());
        }
        if (null != goodsRequest.getIsHide() && !goodsRequest.getIsHide().equals(goodsInfo.getIsHide())) {
            map.put("隐藏0-否 1-是", "修改后：" + goodsRequest.getIsHide());
        }
        if (!StringUtils.isEmpty(goodsInfo.getProductImg()) && !goodsInfo.getProductImg().equals(goodsRequest.getProductImg())) {
            map.put("商品主图", "修改前后：" + goodsInfo.getProductImg() + "-->" + goodsRequest.getProductImg());
        }
        if (null != goodsInfo.getBrandId() && !goodsInfo.getBrandId().equals(goodsRequest.getBrandId())) {
            map.put("品牌", "修改前后：" + goodsInfo.getBrandId() + "-->" + goodsRequest.getBrandId());
        }
        if (!StringUtils.isEmpty(goodsInfo.getLongTitle()) && !goodsInfo.getLongTitle().equals(goodsRequest.getLongTitle())) {
            map.put("长标题", "修改前后：" + goodsInfo.getLongTitle() + "-->" + goodsRequest.getLongTitle());
        }
        if (!StringUtils.isEmpty(goodsRequest.getPlatformData()) && !goodsRequest.getPlatformData().equals(goodsInfo.getPlatformData())) {
            map.put("所属平台", "修改前后：" + goodsInfo.getPlatformData() + "-->" + goodsRequest.getPlatformData());
        }
        if (null != goodsRequest.getBankerColumnId() && !goodsRequest.getBankerColumnId().equals(goodsInfo.getBankerColumnId())) {
            map.put("官网栏目", "修改前后：" + goodsInfo.getBankerColumnId() + "-->" + goodsRequest.getBankerColumnId());
        }
        //旧规格
        List<ProductSpecResponse> oldSpecList = goodsInfo.getSpec();
        //新规格
        List<ProductSpecResponse> newSpecList = goodsRequest.getSpec();

        //旧规格进行拼接
        List<String> oldlist = Lists.newArrayList();
        for (ProductSpecResponse oldSpec : oldSpecList) {
            oldlist.add("sku规格" + oldSpec.getSkuSpec()
                    + "--成本价" + oldSpec.getCostPrice()
                    + "、直接购买价" + oldSpec.getCurPrice()
                    + "、市场价(划线价)" + oldSpec.getOriginalPrice()
                    + "、商品售价" + oldSpec.getPrice()
                    + "、库存" + oldSpec.getStock()
                    + "、图片" + oldSpec.getProImg());
        }
        String old = String.join(",", oldlist);
        //新规格进行拼接
        List<String> newList = Lists.newArrayList();
        for (ProductSpecResponse newSpec : newSpecList) {
            newList.add("sku规格" + newSpec.getSkuSpec()
                    + "--成本价" + newSpec.getCostPrice()
                    + "、直接购买价" + newSpec.getCurPrice()
                    + "、市场价(划线价)" + newSpec.getOriginalPrice()
                    + "、商品售价" + newSpec.getPrice()
                    + "、库存" + newSpec.getStock()
                    + "、图片" + newSpec.getProImg());
        }
        String news = String.join(",", newList);
        //判断规格是否修改
        AtomicBoolean change = new AtomicBoolean(false);
        if (newSpecList.size()==oldSpecList.size()){
            for (int i = 0; i < newSpecList.size(); i++) {
                ProductSpecResponse req = newSpecList.get(i);
                ProductSpecResponse obj = oldSpecList.get(i);
                for (int j = 0; j < newSpecList.size(); j++) {
                    if (!StringUtils.isEmpty(obj.getSkuSpec()) && !obj.getSkuSpec().equals(req.getSkuSpec())) {
                        change.set(true);
                        continue;
                    }
                    if ((null == obj.getCostPrice() ? BigDecimal.ZERO : obj.getCostPrice()).compareTo(null == req.getCostPrice() ? BigDecimal.ZERO : req.getCostPrice()) != 0) {
                        change.set(true);
                        continue;
                    }
                    if ((null == obj.getCurPrice() ? BigDecimal.ZERO : obj.getCurPrice()).compareTo(null == req.getCurPrice() ? BigDecimal.ZERO : req.getCurPrice()) != 0) {
                        change.set(true);
                        continue;
                    }
                    if ((null == obj.getOriginalPrice() ? BigDecimal.ZERO : obj.getOriginalPrice()).compareTo(null == req.getOriginalPrice() ? BigDecimal.ZERO : req.getOriginalPrice()) != 0) {
                        change.set(true);
                        continue;
                    }
                    if ((null == obj.getPrice() ? BigDecimal.ZERO : obj.getPrice()).compareTo(null == req.getPrice() ? BigDecimal.ZERO : req.getPrice()) != 0) {
                        change.set(true);
                        continue;
                    }
                    if (null != obj.getStock() && !obj.getStock().equals(req.getStock())) {
                        change.set(true);
                        continue;
                    }
                    if (!StringUtils.isEmpty(obj.getProImg()) && !obj.getProImg().equals(req.getProImg())) {
                        change.set(true);
                        continue;
                    }
                }
            }
        }else {
            change.set(true);
        }
        if (change.get()) {
            map.put("规格", "修改前：" + old + "-------->修改后：" + news);
        }
        map.put("id", goodsRequest.getId());
        map.put("batch", 0);
        return map;
    }


    @PostMapping("/getEvaluationInfo")
    public CommonResult getEvaluationInfo(@RequestBody GetEvaluationInfoRequest request) {
        return CommonResult.page(goodsService.getEvaluationInfo(request));
    }

    @GetMapping("getEvaluationInfoDetail")
    public CommonResult getEvaluationInfoDetail(@RequestParam Integer productId,
                                                @RequestParam Integer pageNum,
                                                @RequestParam Integer pageSize) {
        return CommonResult.page(goodsService.getEvaluationInfoDetail(productId, pageNum, pageSize));
    }

    @GetMapping("/getEvaluationFeedback")
    public CommonResult getEvaluationFeedback(GetEvaluationFeedbackRequest request) {
        return CommonResult.page(goodsService.getEvaluationFeedback(request));
    }

    @GetMapping("/updateBankerColumn")
    public CommonResult updateBankerColumn(@RequestParam Integer productId,
                                           @RequestParam Integer bankerColumnId) {
        goodsService.updateBankerColumn(productId, bankerColumnId);
        return CommonResult.success();
    }

    @GetMapping("/getEvaluationDetailed")
    public CommonResult getEvaluationDetailed(GetEvaluationFeedbackRequest request) {
        return CommonResult.page(goodsService.getEvaluationDetailed(request));
    }

    @GetMapping("/getEvaluationDetailedInfo")
    public CommonResult getEvaluationDetailedInfo(@RequestParam Integer id,
                                                  @RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize) {
        return CommonResult.page(goodsService.getEvaluationDetailedInfo(id, pageNum, pageSize));
    }
}
